package com.gzc.just.play.last.war.centralserver.matchmaking;

/**
 * 匹配请求 - 表示一个待匹配的玩家
 * 
 * @author AI编程助手
 * @version 1.0
 */
public class MatchmakingRequest {
    
    /**
     * 请求状态
     */
    public enum Status {
        WAITING,        // 等待匹配
        MATCHED,        // 已匹配
        CONFIRMED,      // 已确认
        CANCELLED,      // 已取消
        EXPIRED         // 已过期
    }
    
    private long requestId;
    private long playerId;
    private String playerName;
    private int playerLevel;
    private long requestTime;
    private Status status;
    private int rating;
    private int searchRadius = 500; // 等级范围
    private static final long REQUEST_TIMEOUT = 600000; // 10分钟
    
    /**
     * 构造函数
     * 
     * @param requestId 请求ID
     * @param playerId 玩家ID
     * @param playerName 玩家名称
     * @param playerLevel 玩家等级
     * @param rating 玩家评分
     */
    public MatchmakingRequest(long requestId, long playerId, String playerName, 
                             int playerLevel, int rating) {
        this.requestId = requestId;
        this.playerId = playerId;
        this.playerName = playerName;
        this.playerLevel = playerLevel;
        this.rating = rating;
        this.requestTime = System.currentTimeMillis();
        this.status = Status.WAITING;
    }
    
    /**
     * 检查请求是否已过期
     * 
     * @return 是否过期
     */
    public boolean isExpired() {
        if (status == Status.WAITING) {
            if (System.currentTimeMillis() - requestTime > REQUEST_TIMEOUT) {
                status = Status.EXPIRED;
                return true;
            }
        }
        return false;
    }
    
    /**
     * 获取等待时间（毫秒）
     * 
     * @return 等待时间
     */
    public long getWaitingTime() {
        return System.currentTimeMillis() - requestTime;
    }
    
    /**
     * 标记为已匹配
     * 
     * @return 是否成功
     */
    public boolean markMatched() {
        if (status == Status.WAITING) {
            status = Status.MATCHED;
            return true;
        }
        return false;
    }
    
    /**
     * 标记为已确认
     * 
     * @return 是否成功
     */
    public boolean markConfirmed() {
        if (status == Status.MATCHED) {
            status = Status.CONFIRMED;
            return true;
        }
        return false;
    }
    
    /**
     * 取消匹配请求
     * 
     * @return 是否成功
     */
    public boolean cancel() {
        if (status == Status.WAITING || status == Status.MATCHED) {
            status = Status.CANCELLED;
            return true;
        }
        return false;
    }
    
    /**
     * 检查是否与另一个请求匹配（基于等级和评分）
     * 
     * @param other 另一个请求
     * @return 是否匹配
     */
    public boolean canMatchWith(MatchmakingRequest other) {
        if (other == null || other.getPlayerId() == this.playerId) {
            return false;
        }
        
        // 检查等级范围
        int levelDiff = Math.abs(this.playerLevel - other.playerLevel);
        if (levelDiff > searchRadius) {
            return false;
        }
        
        // 检查评分范围（±1000分以内）
        int ratingDiff = Math.abs(this.rating - other.rating);
        if (ratingDiff > 1000) {
            return false;
        }
        
        return true;
    }
    
    // Getters and Setters
    
    public long getRequestId() {
        return requestId;
    }
    
    public long getPlayerId() {
        return playerId;
    }
    
    public String getPlayerName() {
        return playerName;
    }
    
    public int getPlayerLevel() {
        return playerLevel;
    }
    
    public long getRequestTime() {
        return requestTime;
    }
    
    public Status getStatus() {
        return status;
    }
    
    public int getRating() {
        return rating;
    }
    
    public int getSearchRadius() {
        return searchRadius;
    }
    
    public void setSearchRadius(int searchRadius) {
        this.searchRadius = Math.max(0, searchRadius);
    }
    
    @Override
    public String toString() {
        return String.format("MatchmakingRequest{id=%d, playerId=%d, name='%s', level=%d, status=%s}",
                           requestId, playerId, playerName, playerLevel, status);
    }
}
